package com.qf.java2201.controller;

import com.qf.java2201.common.PageVo;
import com.qf.java2201.common.R;
import com.qf.java2201.pojo.Logistics;
import com.qf.java2201.service.LogisticsService;
import com.qf.java2201.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * ========       ========
 * Time: 2022/4/28
 */
@RestController
@RequestMapping("/logisticsList")
//@CrossOrigin("*")
//@CrossOrigin(origins = {"http://127.0.0.1:5500"}, allowCredentials = "true")  //前后端分离
public class LogisticsListController {

    @Autowired
    private LogisticsService logisticsService; //注入属性

//01-查询数据
    @RequestMapping("/logisticsPageData")

    // 这里 的  方法内的  @RequestParam  不能省略    省略就无法获取参数了
    public PageVo<Logistics> getPageData(@RequestParam Map<String,Object> params){

   /*     System.out.println(params);
        PageVo<Logistics> pageData = logisticsService.getPageData(params);
        System.out.println(pageData+"-----");
        return pageData;*/

        return logisticsService.getPageData(params);
    }

 //02-添加物流信息
    @RequestMapping("/addLogistics")

    public R addLogistics(Logistics logistics){

        //调用service层
        int result =logisticsService.addLogistics(logistics);
        System.out.println(result); //输出信息

        R<Object> r = new R<>();

        if(result==1){
            r.setCode(0);
            r.setMsg("添加成功");
        }
        else{
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }

//03-编辑物流信息数据  (更新 /修改)
    //@RequestMapping("/upload")

    @RequestMapping("/updateLogistics")

    public R updateLogistics(Logistics logistics){
        //验证
        System.out.println("进入了controller控制器层"); //输出信息

        //调用service层
        int result =logisticsService.updateLogistics(logistics);
        R<Object> r = new R<>();

        if(result==1){
            r.setCode(0);
            r.setMsg("添加成功");
        }
        else{
            r.setCode(-1);
            r.setMsg("添加失败");
        }
        return r;
    }


//04- 根据id 删除学生
    @RequestMapping("/deleteLogistics")
    public R deleteStudent(Integer id){
        // 根据 id 删除学生信息
        int result = logisticsService.deleteLogistics(id);
        R<Object> r = new R<>();
        if(result==1){
            r.setCode(0);
            r.setMsg("删除成功");
        }else{
            r.setCode(-1);
            r.setMsg("删除失败");
        }
        return r;
    }

//05-图片上传阿里云 得到地址 返回给前台 upload

    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        // 上传照片

        String filename = file.getOriginalFilename();  // 得到文件的名字

        InputStream inputStream = file.getInputStream();  // 将file 对象转为 输入流

        // 阿里云存储  返回 上传之后的文件路径
        String path = OssUtil.uploadFile(inputStream,filename);

        R<String> r = new R();
        r.setCode(0);
        r.setMsg("上传成功");
        r.setData(path);
        return r;
    }
}
